<template>
  <div class="picture-search-form">
    <!-- 搜索表单 -->
    <a-form layout="inline" :model="searchParams" @finish="doSearch">
      <a-form-item label="关键词" name="searchText">
        <a-input
          v-model:value="searchParams.searchText"
          placeholder="从名称和简介搜索"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="分类" name="category">
        <a-auto-complete
          v-model:value="searchParams.category"
          style="min-width: 180px"
          :options="categoryOptions"
          placeholder="请输入分类"
          allowClear
        />
      </a-form-item>
      <a-form-item label="标签" name="tags">
        <a-select
          v-model:value="searchParams.tags"
          style="min-width: 180px"
          :options="tagOptions"
          mode="tags"
          placeholder="请输入标签"
          allowClear
        />
      </a-form-item>
      <a-form-item label="日期" name="">
        <a-range-picker
          style="width: 400px"
          show-time
          v-model:value="dateRange"
          :placeholder="['编辑开始日期', '编辑结束时间']"
          format="YYYY/MM/DD HH:mm:ss"
          :presets="rangePresets"
          @change="onRangeChange"
        />
      </a-form-item>
      <a-form-item>
        <a-space>
          <a-button type="primary" html-type="submit" style="width: 66px">搜索</a-button>
          <a-button html-type="reset" @click="doClear">重置</a-button>
        </a-space>
      </a-form-item>

    </a-form>
  </div>
</template>
<script lang="ts" setup>
import PictureQueryRequest = API.PictureQueryRequest
import { onMounted, reactive, ref } from 'vue'
import dayjs from 'dayjs'
import { listPictureTagCategoryUsingGet } from '@/api/pictureController.ts'
import { message } from 'ant-design-vue'

interface Props {
  onSearch?: (searchParams: PictureQueryRequest) => void
}

const props = defineProps<Props>()
const dateRange = ref<[]>([])
const categoryOptions = ref<string[]>([])
const tagOptions = ref<string[]>([])

// 搜索条件
const searchParams = reactive<API.PictureQueryRequest>({})

// 获取数据
const doSearch = () => {
  props.onSearch?.(searchParams)
}
/**
 * 日期范围更改时触发
 * @param dates
 * @param dateStrings
 */
const onRangeChange = (dates: any[], dateStrings: string[]) => {
  if (dates.length < 2) {
    searchParams.startEditTime = undefined
    searchParams.endEditTime = undefined
  } else {
    searchParams.startEditTime = dates[0].toDate()
    searchParams.endEditTime = dates[1].toDate()
  }
}

const rangePresets = ref([
  { label: '过去 7 天', value: [dayjs().add(-7, 'd'), dayjs()] },
  { label: '过去 14 天', value: [dayjs().add(-14, 'd'), dayjs()] },
  { label: '过去 30 天', value: [dayjs().add(-30, 'd'), dayjs()] },
  { label: '过去 90 天', value: [dayjs().add(-90, 'd'), dayjs()] },
])

// 获取标签和分类选项
const getTagCategoryOptions = async () => {
  const res = await listPictureTagCategoryUsingGet()
  if (res.data.code === 0 && res.data.data) {
    // 转换成下拉选项组件接受的格式
    tagOptions.value = (res.data.data.tagList ?? []).map((data: string) => {
      return {
        value: data,
        label: data,
      }
    })
    categoryOptions.value = (res.data.data.categoryList ?? []).map((data: string) => {
      return {
        value: data,
        label: data,
      }
    })
  } else {
    message.error('加载选项失败，' + res.data.message)
  }
}
// 清理
const doClear = () => {
  // 取消所有对象的值
  Object.keys(searchParams).forEach((key) => {
    searchParams[key] = undefined
  })
  dateRange.value = []
  props.onSearch?.(searchParams)
}


onMounted(() => {
  getTagCategoryOptions()
})
</script>
<style scoped>
#pictureManagePage {
  max-width: 1600px;
  margin: 0 auto;
  padding: 24px;
  background: #fff;
  min-height: 100vh;
}

/* 标题区域 */
.a-flex[justify='space-between'] {
  margin-bottom: 32px;
  padding-bottom: 16px;
  border-bottom: 1px solid #f0f0f0;
}

h2 {
  margin: 0;
  font-size: 24px;
  color: #1f1f1f;
  font-weight: 600;
}
.picture-search-form .ant-form-item {
  margin-top: 16px;
}


/* 搜索表单 */
.a-form {
  margin: 24px 0;
  background: #fafafa;
  padding: 24px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.a-form-item {
  margin-right: 32px;
  margin-bottom: 0;
}

.a-form-item :deep(.ant-form-item-label) {
  min-width: 60px;
  text-align: right;
}

/* 表格样式 */
.a-table {
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.a-table :deep(th) {
  background: #fafafa !important;
  font-weight: 600;
  color: #1f1f1f;
}

.a-table :deep(td) {
  transition: background 0.3s;
}

.a-table :deep(.ant-table-row:hover td) {
  background: #fafafa !important;
}

/* 图片列 */
.a-image {
  display: block;
  margin: 0 auto;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: transform 0.2s;
}

.a-image:hover {
  transform: scale(1.05);
}

/* 标签样式 */
.a-tag {
  margin: 2px 4px;
  border-radius: 12px;
  background: #f0f0f0;
  color: #666;
}

/* 操作按钮 */
.a-button[type='link'] {
  padding: 0 8px;
  color: #1890ff;
}

.a-button[type='link'][danger] {
  color: #ff4d4f;
}

/* 信息展示区域 */
div[data-index='picInfo'],
div[data-index='review'] {
  line-height: 1.6;
  color: #666;
}

/* 时间列 */
.a-table :deep(.ant-table-cell) > .dayjs-format {
  color: #8c8c8c;
  font-size: 13px;
}

/* 响应式优化 */
@media (max-width: 768px) {
  #pictureManagePage {
    padding: 12px;
  }

  .a-form {
    padding: 16px;
  }

  .a-form-item {
    margin-right: 16px;
  }
}
</style>
